package single.shop.web.freemarker.method;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * 
 * @author Yinyt
 * 
 */
public class EncodeURLMethod implements TemplateMethodModel
{

	private HttpServletResponse response;

	/**
	 * 带参数的构造函数
	 * 
	 * @param response
	 *            HttpServletResponse对象
	 */
	public EncodeURLMethod(HttpServletResponse response)
	{
		this.response = response;
	}

	/**
	 * 实现response.encodeURL(url)功能;(@implements TemplateMethodModel)
	 * 
	 * @param argList
	 *            方法参数列表
	 * @return Object 方法返回值
	 */
	@Override
	public Object exec(List paramList) throws TemplateModelException
	{

		if (paramList.size() != 1) // 限定方法中必须且只能传递一个参数
		{
			throw new TemplateModelException("Wrong arguments!");
		}
		
		// 返回response.encodeURL执行结果
		return response.encodeURL((String) paramList.get(0));
	}

}
